<!DOCTYPE html>
<html>
<title>Registration Page :: DIF233 - Unit 1 - Excercise 1</title>
<body>
	<?php
		$name = "";
		$mobile_number = "";
		$email_id = "";
		
		function update($id, $name, $mobile_number, $email_id){
			$file_data = file("./shadow.txt");
			$file = fopen("./shadow.txt", "w");
			foreach($file_data as $value){
				$tokens = explode(":", $value);
				if(strcmp($tokens[1], $id) == 0){
					$tokens[0] = $name;
					$tokens[3] = $mobile_number;
					$tokens[4] = $email_id;
					$value = $tokens[0].":".$tokens[1].":".$tokens[2].":".$tokens[3].":".$tokens[4]."\n";
				}
				fwrite($file,$value);
			}
		}
		if(!empty($_POST["submit"])){
			update($_POST["id"],$_POST["name"],$_POST["mobile_number"],$_POST["email_id"]);
			echo "<h3>Updated successfully</h3>";
			header("Location: user.php?profile=updated&id=".$_POST["id"]);
			exit();
		}else{
			$file_data = file("./shadow.txt");
			foreach($file_data as $value){
				$tokens = explode(":", $value);
				if(strcmp($tokens[1], $_GET["id"]) == 0){
					$name = $tokens[0];
					$mobile_number = $tokens[3];
					$email_id = $tokens[4];
				}
			}
		}	
	?>
<form method="post" name="profile" action="profile.php?id=<?php echo $_GET['id'] ?>">
<input type="hidden" name="id" value=<?php echo empty($_GET["id"])?$_POST["id"]:$_GET["id"]?> />
	<h3>Please update the details below</h3>
	<table border="0" width="100%">
		<tr>
			<td>
				Name:
			</td>
			<td>
				<input type="text" name="name" value="<?php echo $name ?>" required/>
			</td>
		</tr>
		<tr>
			<td>
				Mobile Number:
			</td>
			<td>
				<input type="text" name="mobile_number" value="<?php echo $mobile_number ?>" required/>
			</td>
		</tr>		
		<tr>
			<td>
				Email Id:
			</td>
			<td>
				<input type="email" name="email_id" value="<?php echo $email_id ?>" required/>
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" name="submit" value="submit"/></td>
		</tr>		
	</table>
</form>
</body>
</html>
